<?php

namespace common\models;

use common\helpers\commonApi;
use Yii;

/**
 * This is the model class for table "{{%goods}}".
 *
 * @property int $id
 * @property string|null $name 名称
 * @property int|null $classify_id 分类id
 * @property string|null $main_img 主图
 * @property string|null $imgs 详情轮播图
 * @property int|null $vr_sales 虚拟销量
 * @property int|null $ac_sales 实际销量
 * @property string|null $intr 简介
 * @property float|null $price 价格
 * @property string|null $details 详情
 * @property int|null $status 状态
 * @property string|null $create_time 创建时间
 * @property string|null $create_user 创建人
 * @property string|null $update_time 修改时间
 * @property string|null $update_user 修改人
 * @property int|null $delete 删除
 * @property int|null $sort 排序
 */
class Goods extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%goods}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['classify_id', 'vr_sales', 'ac_sales', 'status', 'delete', 'sort'], 'integer'],
            [['intr', 'details','imgs'], 'string'],
            [['price'], 'number'],
            [['create_time', 'update_time'], 'safe'],
            [['name', 'main_img', 'create_user', 'update_user'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '名称',
            'classify_id' => '分类id',
            'main_img' => '主图',
            'imgs' => '详情轮播图',
            'vr_sales' => '虚拟销量',
            'ac_sales' => '实际销量',
            'intr' => '简介',
            'price' => '价格',
            'details' => '详情',
            'status' => '状态',
            'create_time' => '创建时间',
            'create_user' => '创建人',
            'update_time' => '修改时间',
            'update_user' => '修改人',
            'delete' => '删除',
            'sort' => '排序',
        ];
    }

    /**
     * 验证数据
     * @param $data
     * @return array
     */
    public function verif($data)
    {
        if (empty($data['name'])){
            return commonApi::message('请输入商品名称',false,['id'=>0]);
        }
        if (empty($data['classify_id'])){
            return commonApi::message('请选择商品分类',false,['id'=>0]);
        }
        if (empty($data['main_img'])){
            return commonApi::message('请上传商品主图',false,['id'=>0]);
        }
        if (empty($data['imgs'])){
            return commonApi::message('请上传商品详情页轮播图',false,['id'=>0]);
        }
        $data['vr_sales'] = $data['vr_sales']??0;
        if ($data['vr_sales']<0){
            return commonApi::message('虚拟销量必须大于或等于0',false,['id'=>0]);
        }

        foreach ($data['specAll'] as $key=>&$datum){
            $h = $key+1;
            if (empty($datum['name'])){
                return commonApi::message('第'.$h.'行：请输入规格名称',false,['id'=>1]);
            }
            if (empty($datum['price'])){
                return commonApi::message('第'.$h.'行：请输入规格价格',false,['id'=>1]);
            }
            $datum['trade_price'] = $datum['trade_price']??0;
            $datum['ca_price'] = $datum['ca_price']??0;
            $datum['good_id'] = 'my_good_id';
        }
        $data['intr'] = $data['intr']??'';
        $data['details'] = $data['details']??'';
        $arr = [
            'name'=>$data['name'],
            'classify_id'=>$data['classify_id'],
            'main_img'=>$data['main_img'],
            'imgs'=>$data['imgs'],
            'vr_sales'=>$data['vr_sales'],
            'intr'=>$data['intr'],
            'price'=>$data['price'],
            'details'=>$data['details'],
            'sort'=>$data['sort'],
            'status'=>$data['status'],
            'specAll'=>$data['specAll'],
        ];
        return commonApi::message('ok',true,$arr);
    }

    public function getSpec()
    {
        return $this->hasMany(GoodsSpec::className(),['good_id'=>'id']);
    }
}
